package com.itqf.handler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * projectName: sm_project
 *
 * @author: 赵伟风
 * time: 2021/7/13 14:57
 * description:定义类型转化器 字符串和整数数组互相转换!
 */
//转化成的java数据类型
@MappedTypes(Integer[].class)
//数据库的数据类型
@MappedJdbcTypes(JdbcType.VARCHAR)
public class StringToIntegerArrayTypeHandler extends BaseTypeHandler<Integer[]> {

    //jackjson包中的工具类,可以字符串和数组之间互相转化!
    public static  final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 插入数据!  Integer[]  - String
     * @param preparedStatement
     * @param i
     * @param integers
     * @param jdbcType
     * @throws SQLException
     */
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Integer[] integers, JdbcType jdbcType) throws SQLException {

        /**
         * objectMapper writer 将其他类型写成字符串类型
         *              reader 将字符串类型转化成其他类型
         */
        String str = null;

        try {
            str = objectMapper.writeValueAsString(integers);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        //将转化后的字符串放入的占位符上!进行插入!
        preparedStatement.setObject(i, str);
    }

    /**
     * 查询  字符串 - Integer[]
     * @param resultSet
     * @param columnName 类名
     * @return
     * @throws SQLException
     */
    @Override
    public Integer[] getNullableResult(ResultSet resultSet, String columnName) throws SQLException {

        /**
         * 参数1: 字符串
         * 参数2: 要转化的类型
         */
        Integer[] ids = new Integer[0];
        try {
            ids = objectMapper.readValue(resultSet.getString(columnName), Integer[].class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ids;
    }

    @Override
    public Integer[] getNullableResult(ResultSet resultSet, int columnIndex) throws SQLException {

        /**
         * 参数1: 字符串
         * 参数2: 要转化的类型
         */
        Integer[] ids = new Integer[0];
        try {
            ids = objectMapper.readValue(resultSet.getString(columnIndex), Integer[].class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ids;
    }

    @Override
    public Integer[] getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        /**
         * 参数1: 字符串
         * 参数2: 要转化的类型
         */
        Integer[] ids = new Integer[0];
        try {
            ids = objectMapper.readValue(callableStatement.getString(i), Integer[].class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ids;
    }
}
